<?php
/**
 * Created by PhpStorm
 * User: rain
 * Date: 2018/12/26
 * Time: 下午5:23
 */

namespace App\Models;

use App\Utils\FileUtil;
use Illuminate\Database\Eloquent\Model;

/**
 * Class Book
 * @property integer $id
 * @property string $cover
 * @property string $name
 * @property string $author
 * @property integer $word_ct
 * @property integer $study_ct
 * @property integer $complete_ct
 * @property integer $origin_price
 * @property integer $price
 * @property integer $rate
 * @property integer $active_ct
 * @property integer $new_ct
 * @property integer $auth_ct
 * @property integer $pay_ct
 * @property integer $pay_chapter_ct
 * @property integer $pay_all_chapter_ct
 * @property integer $member_ct
 * @property integer $income
 * @property string $created_at
 * @property string $updated_at
 * @property string $share_card
 * @property string $share_title
 * @package App\Models
 */
class Book extends Model
{
    protected $table   = 'book';
    protected $guarded = ['id'];

    public static $showFields = ['id', 'cover', 'name', 'word_ct', 'share_card', 'share_title', 'price'];

    public function getCoverAttribute($value)
    {
        return FileUtil::getDomain($value);
    }

    public function getShareCardAttribute($value)
    {
        return FileUtil::getDomain($value);
    }

    public function chapters()
    {
        return $this->hasMany(BookChapter::class);
    }

    public function words()
    {
        return $this->hasMany(BookWord::class, 'book_id', 'id');
    }

    public function classifies()
    {
        return $this->belongsToMany(Classify::class, 'classify_book', 'book_id', 'classify_id')->withPivot('sort')->orderByPivot('sort', 'asc');
    }

}
